#define  _CRT_SECURE_NO_WARNINGS
#include"stack.h"
void Stackinit(stack* s)
{
	s->a = (datatype*)malloc(sizeof(datatype) * 4);
	s->capacity = 4;
	s->top = 0;
}

void Stackdestroy(stack* s)
{
	s->capacity = 0;
	s->top = 0;
	free(s->a);
	s->a = NULL;
}

void Stackpush(stack* s, datatype x)
{
	if (s->capacity == s->top)
	{
		datatype* new = (datatype*)realloc(s->a, s->capacity * 2 * sizeof(datatype));
		if (new == NULL)
			exit(-1);
		else
		{
			s->a = new;
			s->capacity *= 2;
		}
	}
	s->a[s->top] = x;
	s->top++;
}

void Stackpop(stack* s)
{
	if(s!=NULL&& !Stackempty(s))
	s->top--;
}

int Stackgettop(stack* s)
{
	if (s != NULL && !Stackempty(s))
		return s->a[s->top - 1];
	else
		return 0;
}

int stacklength(stack* s)
{
	return s->top;
}

int Stackempty(stack* s)
{
	if (s != NULL)
	{
		if (s->top == 0)
			return 1;
		else
			return 0;
	}
}

void Stackclear(stack* s)
{
	if (s != NULL && !Stackempty(s))
		s->top == 0;
}